﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SMS.DAL.DAHelpers;
using SMS.Entities;

namespace SMS.DAL
{
    public class CategoryDA : BaseDA<Category>
    {
        public override Category SaveObject(string userName, Category tObject)
        {
            #region Insert/Update Procedure
            /*
            ALTER PROCEDURE [dbo].[USP_INS_UPDATE_CATEGORY]  
(@Category_ID int,  
@Category_Name varchar(50),  
@IsLive bit)  
as  
IF (@Category_ID=0)  
BEGIN  
INSERT INTO [Shop].[dbo].[Category]   
		  (       
			  [Category_Name]
			  ,[IsLive])  
     VALUES  
           (  
		   @Category_Name,  
		   @IsLive )  
     SELECT @@IDENTITY  
END  
ELSE  
BEGIN             
UPDATE [Shop].[dbo].[Category]  
   SET [Category_Name] = @Category_Name       
      ,[IsLive] = @IsLive  
 WHERE Category_ID=@Category_ID  
 END
            */
            #endregion

            try
            {
                dbHelper.AddParameter("@Category_ID", tObject.Category_ID);
                dbHelper.AddParameter("@Category_Name", tObject.Category_Name);
                dbHelper.AddParameter("@IsLive", tObject.IsLive);
                object objID = dbHelper.ExecuteScaler("USP_INS_UPDATE_CATEGORY", System.Data.CommandType.StoredProcedure, System.Data.ConnectionState.Closed);
                if (Convert.ToInt32(objID) > 0)
                    tObject.Category_ID = Convert.ToInt32(objID);
                LogActivity(userName, string.Format("Category Saved with ID {0}", tObject.Category_ID), tObject);
                return tObject;
            }
            catch (Exception ex)
            {
                LogException(userName, ex, tObject);
                throw new Exception("Error Occured while Saving Category");
            }
        }

        public override bool DeleteObject(string userName, Category tObject)
        {
            throw new NotImplementedException();
        }

        public override List<Category> GetObjectList(string userName, DateTime fromDate, DateTime toDate)
        {
            throw new NotImplementedException();
        }

        public override List<Category> SearchObject(string userName, string SearchText)
        {
            #region Procedure
            /* CREATE PROCEDURE [dbo].[USP_SEARCH_CATEGORY] 
(@SearchText VARCHAR(256))  
AS  
SELECT [Category_ID]
      ,[Category_Name]
      ,[IsLive]
  FROM [shop].[dbo].[Category]  
  where IsLive=1 and ([Category_ID] LIKE '%'+ @SearchText+'%'  
  or [Category_Name] LIKE '%'+ @SearchText+'%')  
  order by [Category_Name],[Category_ID]   */
            #endregion

            try
            {
                dbHelper.AddParameter("@SearchText", string.IsNullOrWhiteSpace(SearchText) ? string.Empty : SearchText);
                var reader = dbHelper.ExecuteReader("USP_SEARCH_CATEGORY", System.Data.CommandType.StoredProcedure, System.Data.ConnectionState.Closed);
                var list = new List<Category>();
                while (reader.Read())
                {
                    list.Add(GetCategoryObject(reader));
                }
                LogActivity(userName, string.Format("Searched Category with text {0}", SearchText), null);
                return list;
            }
            catch (Exception ex)
            {
                LogException(userName, ex, null);
                throw new Exception("Error while searching Category");
            }
        }

        private Category GetCategoryObject(System.Data.Common.DbDataReader reader)
        {
             int i = 0;
            Category obj = new Category();
            obj.Category_ID = reader.GetInt32(i++);
            obj.Category_Name = reader.GetString(i++);            
            obj.IsLive = reader.GetBoolean(i++);
            return obj;
        }
        
    }
}
